﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Common;

namespace MyWeb.Controls
{
    public partial class admLeft : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                string strReturn = string.Empty;
                List<Data.Module> list = new List<Data.Module>();
                if (Common.StringClass.Check(Session["UserId"]) && Session["IsAdmin"].ToString().Equals("0"))
                {
                    list = Business.ModuleService.Module_GetByAll();
                }
                else
                {
                    list = Business.ModuleService.Module_GetByTop("", "Id in (Select ModuleId From Permission Where [Right] not like '0%')", "Level, Ord");
                }
                int k = 0;
                for (int i = 0; i < list.Count; i++)
                {
                    if (i > 0 && list[i - 1].Level.Length == 10 && list[i].Level.Length == 5)
                    {
                        strReturn += string.Format("</ul></div>");
                    }
                    if (list[i].Level.Length == 5)
                    {
                        k++;
                        strReturn += string.Format("<table class=\"table\"><tr><td class=\"left\"><img alt=\"\" src=\"{2}blank.gif\" /></td><td>{0}</td><td class=\"image\"><img alt=\"\" id=\"imgdiv{1}\" src=\"{2}closed.gif\" onclick=\"toggleXPMenu('div{1}');\"/></td><td class=\"right\"><img alt=\"\" src=\"{2}blank.gif\" /></td></tr></table>", list[i].Name, k, GlobalClass.GetUrlAdminImage());
                        strReturn += string.Format("<div id=\"div{0}\" class=\"content\"><ul>",k);
                    }
                    if (list[i].Level.Length == 10)
                    {
                        string strClass = Request.Url.PathAndQuery.Contains(list[i].Page) ? " class=\"select_mod\"" : string.Empty;
                        strReturn += string.Format("<li" + strClass + "><img alt=\"\" src=\"{0}\"><a href=\"{1}\">{2}</a></li>", list[i].Image, GlobalClass.GetUrlAdmin() + list[i].Page, list[i].Name);
                    }
                    if (i == list.Count - 1)
                    {
                        strReturn += string.Format("</ul></div>");
                    }
                }
                ltrModule.Text = strReturn;
            }
        }
    }
}